<template>
  <view>
    <u-navbar
      v-if="!$isWeixin()"
      :customBack="backToList"
      :titleBold="true"
      :title-width="500"
      :title="DealerInfo.Dealer_BaseInfo.Name"
    ></u-navbar>
    <!-- <u-subsection :list="tabList" @change="onTabChange" active-color="#FF7A23" :current="currentTab"></u-subsection> -->
    <view class="tab-list" v-if="Level == 1">
      <text
        class="tab-item"
        @click="onTabChange(index)"
        :class="{ active: currentTab === index }"
        v-for="(item, index) in tabList"
        :key="index"
      >
        {{ item.name }}
      </text>
    </view>

    <view v-if="currentTab === 0">
      <u-tabs active-color="#FF7A23" :current="subTab" :list="subTabList" @change="onSubTabChange"></u-tabs>

      <view class="" v-if="subTab == 0">
        <view class="" v-if="Level == 2">
          <u-cell-item
            title="一级经销商名称"
            :arrow="false"
            :value="DealerInfo.Dealer_BaseInfo.Parent_Name"
          ></u-cell-item>
        </view>
        <view class="" v-if="Level == 1 || Level == 2">
          <basic-information
            :loadCity="loadCity"
            :level="Level"
            :metadata="DataBaseInfo"
            :showParent="false"
            :data="DealerInfo"
          />
        </view>

        <view class="" v-if="Level == 3">
          <u-cell-item
            title="二级经销商名称"
            :arrow="false"
            :value="DealerInfo.Dealer_BaseInfo.Parent_Name"
          ></u-cell-item>
          <store-information :loadCity="loadCity" :data="DealerInfo.Dealer_BaseInfo"></store-information>
        </view>
      </view>
      <relations :list="Dealer_RelationList" v-if="subTab === 1"></relations>
    </view>

    <jindie-information :data="Ke_DealerInfo" :metadata="Ke_DataBaseInfo" v-if="currentTab === 1"></jindie-information>

    <view class="u-p-30">
      <u-button
        type="warning"
        @click="
          $u.route({
            type: 'navigateBack'
          })
        "
      >
        返回列表
      </u-button>
    </view>
  </view>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      currentTab: 0,
      subTab: 0,
      subTabList: [{ name: '基础信息' }, { name: '销售业务关系' }],
      DealerId: 114,
      Level: 1,
      loadCity: false,
      // 元数据
      DataBaseInfo: {
        BusinessCategory_BaseList: [],
        CompetingGoodsBrandA_BaseList: [],
        CompetingGoodsBrandB_BaseList: [],
        CompetingGoodsBrandC_BaseList: [],
        DealerApplyFor_BaseList: [],
        DealerGrade_BaseList: [],
        DealerMaterialRequirements_BaseList: [],
        DealerShutDown_BaseList: [],
        DealerType_BaseList: [],
        DealerAndGoReason_BaseList: []
      },
      Ke_DataBaseInfo: {
        BusinessCategory_BaseList: [],
        CompetingGoodsBrandA_BaseList: [],
        CompetingGoodsBrandB_BaseList: [],
        CompetingGoodsBrandC_BaseList: [],
        DealerApplyFor_BaseList: [],
        DealerGrade_BaseList: [],
        DealerMaterialRequirements_BaseList: [],
        DealerShutDown_BaseList: [],
        DealerType_BaseList: [],
        DealerAndGoReason_BaseList: []
      },
      DealerInfo: {
        Dealer_BaseInfo: {
          Account: '',
          Address: '',
          CityId: null,
          Created: null,
          DealerId: null,
          Delivery_Address: '',
          Delivery_CityId: null,
          Delivery_DistrictId: null,
          Delivery_ProvinceId: null,
          DistrictId: null,
          IsFictitious: null,
          KeDealerNo: '',
          Level: 1,
          LevelName: '',
          Mobile: '',
          Name: '',
          NickName: '',
          Parent_DealerId: 90,
          Parent_Name: '',
          ProvinceId: 610000,
          Referred: '',
          State: 4,
          Telephone: ''
        },
        Dealer_OtherInfo: {
          BlessingNote: '',
          BossBirthday: null,
          BossName: '',
          BossPhone: '',
          BusinessCategory: '',
          BusinessLicenseA: '',
          CompetingGoodsBrandA: '5,6,8,',
          CompetingGoodsBrandB: '47,49,50,',
          CompetingGoodsBrandC: '64,65,67,70,71,',
          DealerGrade: 1,
          DealerType: 1,
          IsSign: 2,
          IsVIP: 1,
          LastYearsBDRSales: 14001,
          PeopleNum: 25,
          PercentageBDRSales: 35,
          TotalSales: 40001,
          TreatyPhoto: '',
          TreatyPhotoType: 1
        },
        DealerDetail_Audit: {
          ApplayRemark: '',
          ApplayTime: '',
          AuditState: 1,
          AuditStateName: '',
          FinalAuditNote: '',
          FirstAuditNote: '',
          ReviewAuditNote: ''
        },
        AuditForList: []
      },
      // 温改销售业务关系
      Dealer_RelationList: [],
      Ke_DealerInfo: {
        Ke_Dealer_BaseInfo: {
          PartsLevel: 'A',
          KeLevelName: '一级',
          Channel: '',
          DealerId: 8871,
          Name: '南京跨迹商贸有限公司',
          Referred: '南京跨迹',
          Account: '',
          KeDealerNo: '01000657',
          NickName: '魏凯',
          Mobile: '186-5201-6912',
          Telephone: ' ',
          Level: 0,
          LevelName: '',
          ProvinceId: 320000,
          CityId: 320100,
          DistrictId: 320106,
          Address: ' ',
          Delivery_ProvinceId: 0,
          Delivery_CityId: 0,
          Delivery_DistrictId: 0,
          Delivery_Address: '',
          Parent_DealerId: 0,
          Parent_Name: '',
          State: 1,
          Created: 1615789906,
          IsFictitious: 0
        },
        Ke_Dealer_OtherInfo: {
          DealerType: 0,
          BusinessCategory: '',
          CompetingGoodsBrandA: '',
          CompetingGoodsBrandB: '',
          CompetingGoodsBrandC: '',
          LastYearsBDRSales: 0,
          TotalSales: 0,
          PercentageBDRSales: 0,
          PeopleNum: 0,
          DealerGrade: 0,
          BlessingNote: '',
          BossName: '',
          BossPhone: '',
          BossBirthday: 0,
          IsVIP: 0,
          BusinessLicenseA: '',
          TreatyPhoto: '',
          TreatyPhotoType: 1,
          IsSign: 0
        },
        Ke_Dealer_RelationList: [],
        Ke_DealerInvoiceInfo: {
          Rise: '',
          BankName: '',
          BankNumber: '',
          InvoiceAdress: '',
          InvoicePhone: '',
          Remark: ''
        }
      }
    };
  },

  computed: {
    tabList() {
      if (this.Level == 1) {
        return [{ name: '温暖改造家' }, { name: '金蝶' }];
      } else {
        return [{ name: '温暖改造家' }];
      }
    }
  },

  onLoad(options) {
    console.log(options);
    this.DealerId = options.DealerId;
    this.Level = options.Level;
    this.title = options.title;

    this.$u.api
      .getDealerDetail({
        DealerId: this.DealerId,
        SelectType: 0
      })
      .then(res => {
        console.log(res);
        const {
          ResultCode,
          ResultMessage,
          DataBaseInfo,
          Dealer_BaseInfo,
          Dealer_OtherInfo,
          Dealer_RelationList,
          Ke_DealerInvoiceInfo,
          Ke_Dealer_RelationList,
          Ke_Dealer_OtherInfo,
          Ke_Dealer_BaseInfo,
          AuditForList
        } = res;
        if (ResultCode === 1) {
          this.DataBaseInfo = JSON.parse(JSON.stringify(DataBaseInfo));
          this.Ke_DataBaseInfo = JSON.parse(JSON.stringify(DataBaseInfo));

          if (this.Level != 3) {
            this.DataBaseInfo.BusinessCategory_BaseList.forEach(item => {
              item.checked = Dealer_OtherInfo.BusinessCategory.split(',').indexOf(String(item.Key)) > -1;
            });
            this.DataBaseInfo.CompetingGoodsBrandA_BaseList.forEach(item => {
              item.checked = Dealer_OtherInfo.CompetingGoodsBrandA.split(',').indexOf(String(item.Key)) > -1;
            });
            this.DataBaseInfo.CompetingGoodsBrandB_BaseList.forEach(item => {
              item.checked = Dealer_OtherInfo.CompetingGoodsBrandB.split(',').indexOf(String(item.Key)) > -1;
            });
            this.DataBaseInfo.CompetingGoodsBrandC_BaseList.forEach(item => {
              item.checked = Dealer_OtherInfo.CompetingGoodsBrandC.split(',').indexOf(String(item.Key)) > -1;
            });
          }

          if (this.Level == 1) {
            this.Ke_DataBaseInfo.BusinessCategory_BaseList.forEach(item => {
              if (Ke_Dealer_OtherInfo && Ke_Dealer_OtherInfo.BusinessCategory) {
                item.checked = Ke_Dealer_OtherInfo.BusinessCategory.split(',').indexOf(String(item.Key)) > -1;
              } else {
                item.checked = false;
              }
            });
            this.Ke_DataBaseInfo.CompetingGoodsBrandA_BaseList.forEach(item => {
              if (Ke_Dealer_OtherInfo && Ke_Dealer_OtherInfo.CompetingGoodsBrandA) {
                item.checked = Ke_Dealer_OtherInfo.CompetingGoodsBrandA.split(',').indexOf(String(item.Key)) > -1;
              } else {
                item.checked = false;
              }
            });
            this.Ke_DataBaseInfo.CompetingGoodsBrandB_BaseList.forEach(item => {
              if (Ke_Dealer_OtherInfo && Ke_Dealer_OtherInfo.CompetingGoodsBrandB) {
                item.checked = Ke_Dealer_OtherInfo.CompetingGoodsBrandB.split(',').indexOf(String(item.Key)) > -1;
              } else {
                item.checked = false;
              }
            });
            this.Ke_DataBaseInfo.CompetingGoodsBrandC_BaseList.forEach(item => {
              if (Ke_Dealer_OtherInfo && Ke_Dealer_OtherInfo.CompetingGoodsBrandC) {
                item.checked = Ke_Dealer_OtherInfo.CompetingGoodsBrandC.split(',').indexOf(String(item.Key)) > -1;
              } else {
                item.checked = false;
              }
            });
          }

          this.DataBaseInfo = DataBaseInfo;
          this.DealerInfo.Dealer_BaseInfo = Dealer_BaseInfo;
          this.DealerInfo.Dealer_OtherInfo = Dealer_OtherInfo;
          this.DealerInfo.AuditForList = AuditForList;
          this.Dealer_RelationList = Dealer_RelationList;

          if (Ke_DealerInvoiceInfo) {
            this.Ke_DealerInfo.Ke_DealerInvoiceInfo = Ke_DealerInvoiceInfo;
          }
          if (Ke_Dealer_BaseInfo) {
            this.Ke_DealerInfo.Ke_Dealer_BaseInfo = Ke_Dealer_BaseInfo;
          }
          if (Ke_Dealer_OtherInfo) {
            this.Ke_DealerInfo.Ke_Dealer_OtherInfo = Ke_Dealer_OtherInfo;
          }
          if (Ke_Dealer_RelationList) {
            this.Ke_DealerInfo.Ke_Dealer_RelationList = Ke_Dealer_RelationList;
          }
          this.loadCity = true;

          if (this.$isWeixin()) {
            this.$setTitle(this.DealerInfo.Dealer_BaseInfo.Name);
          }
        }
      });
  },
  methods: {
    backToList() {
      if (this.Level == 1) {
        this.$u.route({
          url: '/pages/home/dealer/index'
        });
        return false;
      } else {
        this.$u.route({
          url: '/pages/home/dealer/index',
          params: {
            Level: this.Level,
            ParentId: this.DealerInfo.Dealer_BaseInfo.Parent_DealerId
          }
        });
      }
    },

    onTabChange(index) {
      this.currentTab = index;
    },

    onSubTabChange(index) {
      this.subTab = index;
    }
  }
};
</script>

<style lang="scss">
.tab-list {
  display: flex;
  flex-direction: row;

  .tab-item {
    flex: 1;
    max-width: 50%;
    display: inline-block;
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
  }

  .tab-item.active {
    background-color: #ff7a23;
    font-size: 28rpx;
    font-family: NotoSansHans;
    font-weight: 400;
    color: #ffffff;
  }
}
</style>
